package br.idez.idezJet.control.converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.idez.idezJet.integracao.vo.CPF;



@FacesConverter(forClass=CPF.class)
public class CpfConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		CPF cpf = new CPF();
		if(value != null){
			String numCpf = value.substring(0,9);
			String dv = value.substring(9,11);
			
			
			cpf.setNumero(numCpf);
			cpf.setDv(dv);
			
		}else{
			FacesMessage message = new FacesMessage(
			"campo.nulo");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
		}
		
		return cpf;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		CPF cpf = (CPF) value;
		String numCpf = cpf.getNumero();
		String dv = cpf.getDv();
		
		if(numCpf == null && dv == null){
			return "";
		}
		
		return numCpf + dv;
	}
	

}
